
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.research8yfcs.struts.actions;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.manager.commons.config.ConfigFactory;
import com.manager.commons.page.PageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.CommonUtil;
import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.research8yfcs.entity.Research8yfcs;
import com.application.research8yfcs.struts.form.Research8yfcsForm;
import com.application.research8yfcs.service.Research8yfcsService;
import com.application.research8yfcs.commons.Constants;

/**
 * 
 * Description:Research8yfcsAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class Research8yfcsAction extends DispatchAction {

	/**
	 * Service层实例
	 */
	private Research8yfcsService research8yfcsService;
	private AttachmentService attachmentService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
	private Research8yfcs research8yfcs;

	/**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getResearch8yfcsPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchResearch8yfcs" + "开始");
		Research8yfcsForm research8yfcs = (Research8yfcsForm) form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List research8yfcsList = null;
		resultSize = research8yfcsService.getAllCountResearch8yfcs(research8yfcs);
		pageBean = PageBean.getPageBean("research8yfcsTab", resultSize, request);
		research8yfcsList = research8yfcsService.searchResearch8yfcs(research8yfcs,
				pageBean);
		request.setAttribute("research8yfcsList", research8yfcsList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchResearch8yfcs" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddResearch8yfcs(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toAddResearch8yfcss");
		Research8yfcsForm research8yfcsForm = new Research8yfcsForm();
		research8yfcsForm.setId(RandomKey.generateID_Long_19());
		research8yfcsForm.setCreatedate(new Date());
		request.setAttribute("research8yfcsForm", research8yfcsForm);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addResearch8yfcs(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addResearch8yfcss");
		Research8yfcs research8yfcs = this.getResearch8yfcs(form);
		research8yfcs.setCreatedate(new Date());
		this.research8yfcsService.insertObject(research8yfcs);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteResearch8yfcs(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("deleteResearch8yfcss");
		Research8yfcsForm research8yfcsForm = (Research8yfcsForm) form;
		this.research8yfcsService.deleteResearch8yfcss(research8yfcsForm);
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(research8yfcsForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_XMDY);
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateResearch8yfcs(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("toUpdateResearch8yfcss");
		Research8yfcsForm research8yfcsForm = (Research8yfcsForm) form;
		research8yfcs = (Research8yfcs) this.research8yfcsService.searchResearch8yfcs(
				research8yfcsForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, research8yfcs);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateResearch8yfcs(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("updateResearch8yfcss");
		Research8yfcs research8yfcs = this.getResearch8yfcs(form);
		research8yfcs.setCreatedate(new Date());
		this.research8yfcsService.updateObject(research8yfcs);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewResearch8yfcs(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("viewResearch8yfcs");
		Research8yfcsForm research8yfcsForm = (Research8yfcsForm) form;
		research8yfcs = (Research8yfcs) this.research8yfcsService.searchResearch8yfcs(
				research8yfcsForm).get(0);
		request.setAttribute(Constants.DEPARTMENT, research8yfcs);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the Research8yfcsService
	 */
	public Research8yfcsService getResearch8yfcsService() {
		return this.research8yfcsService;
	}

	/**
	 * @param Research8yfcsService
	 *            the Research8yfcsService to set
	 */
	public void setResearch8yfcsService(Research8yfcsService research8yfcsService) {
		this.research8yfcsService = research8yfcsService;
	}

	/**
	 * 获取Research8yfcs实体
	 * 
	 * @param ActionForm
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @return the Research8yfcs
	 */
	public Research8yfcs getResearch8yfcs(ActionForm form)
			throws IllegalAccessException, InvocationTargetException {
		Research8yfcsForm research8yfcsForm = (Research8yfcsForm) form;
		research8yfcs = new Research8yfcs();
		BeanUtils.copyProperties(research8yfcs, research8yfcsForm);
		return research8yfcs;
	}

	public void setResearch8yfcs(Research8yfcs research8yfcs) {
		this.research8yfcs = research8yfcs;
	}

	/**
	 * @return the attachmentService
	 */
	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	/**
	 * @param attachmentService the attachmentService to set
	 */
	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	/**
	 * @return the research8yfcs
	 */
	public Research8yfcs getResearch8yfcs() {
		return research8yfcs;
	}

//	public ActionForward creatTreeSelect(ActionMapping mapping,
//			ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) throws IOException {
//		response.setContentType("text/text;charset=UTF-8");
//		// System.out.println(request.getParameter("pid"));
//		Research8yfcsForm research8yfcsForm = new Research8yfcsForm();
//		research8yfcsForm.setFid(request.getParameter("pid"));
//		String vid = request.getParameter("treeid");
//		int tjs = Integer.parseInt(request.getParameter("js"));
//		Long pid = Long.parseLong(request.getParameter("pid"));
//		List<Research8yfcs> accList = research8yfcsService
//				.searchResearch8yfcs(research8yfcsForm);
//		String html = creatTreeSelect(accList, vid, tjs);
//		java.io.PrintWriter out = response.getWriter();
//		out.print(html);
//		out.flush();
//		out.close();
//		return null;
//	}
//
//	public String creatTreeSelect(List<Research8yfcs> accList, String vid, int js) {
//		String html = "";
//		for (int i = 0; i < accList.size(); i++) {
//			Research8yfcs research8yfcs = (Research8yfcs) accList.get(i);
//			for (int j = 0; j < js; j++) {
//				html = html + "<img src='images/line.gif'>";
//			}
//			html = html + "<img src='images/plus.gif'  id='imgt" + vid + js + i
//					+ "'  onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + research8yfcs.getId() + "');\" >"
//					+ "<img src='images/close1.gif' id='img" + vid + js + i
//					+ "' onclick=\"ajaxProcessBase('" + vid + js + i + "' ,"
//					+ (js + 1) + ",'" + research8yfcs.getId()
//					+ "');\" style=\"cursor:hand\">&nbsp;";
//
//			html = html + "<a onclick=\"selectTree('" + research8yfcs.getId()
//					+ "','" + research8yfcs.getName()
//					+ "')\" style=\"cursor:hand\">" + research8yfcs.getName()
//					+ "</a>";
//			html = html + "<div id='" + vid + js + i
//					+ "' ><span class='f1'>.</span></div>";
//		}
//		return html;
//	}

}
